package com.fei.cloudeurekaprovide.controller;

import com.fei.cloudeurekaprovide.domain.User;
import com.fei.cloudeurekaprovide.service.UserService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    @HystrixCommand(fallbackMethod = "getDefaultUser")
    @RequestMapping("/getUserById/{userId}")
    public User getUserById(@PathVariable("userId") Integer userId){
        User user = this.userService.getUserById(userId);
        if(user == null){       // 测试断路器
            throw new RuntimeException("参数越界");
        }
        return user;
    }

    public User getDefaultUser(Integer userId){
        User user = new User();
        user.setId(userId);
        user.setAge(0);
        user.setUserName("断路器");
        return user;
    }

}
